#!/usr/bin/env python
import sys
import warnings

from datetime import datetime

from note_mind.crew import NoteMind
from tech_article_service import TechArticleService
from dotenv import load_dotenv

warnings.filterwarnings("ignore", category=SyntaxWarning, module="pysbd")

# This main file is intended to be a way for you to run your
# crew locally, so refrain from adding unnecessary logic into this file.
# Replace with inputs you want to test with, it will automatically
# interpolate any tasks and agents information

title = "OpenAI upgrades its transcription and voice-generating AI models"
content = """
OpenAI is bringing new transcription and voice-generating AI models to its API that the company claims improve upon its previous releases.\n\nFor OpenAI, the models fit into its broader “agentic” vision: building automated systems that can independently accomplish tasks on behalf of users. The definition of “agent” might be in dispute, but OpenAI Head of Product Olivier Godement described one interpretation as a chatbot that can speak with a business’s customers.\n\n“We’re going to see more and more agents pop up in the coming months” Godement told TechCrunch during a briefing. “And so the general theme is helping customers and developers leverage agents that are useful, available, and accurate.”\n\nOpenAI claims that its new text-to-speech model, “gpt-4o-mini-tts,” not only delivers more nuanced and realistic-sounding speech but is also more “steerable” than its previous-gen speech-synthesizing models. Developers can instruct gpt-4o-mini-tts on how to say things in natural language — for example, “speak like a mad scientist” or “use a serene voice, like a mindfulness teacher.”\n\nHere’s a “true crime-style,” weathered voice:\n\nAnd here’s a sample of a female “professional” voice:\n\nJeff Harris, a member of the product staff at OpenAI, told TechCrunch that the goal is to let developers tailor both the voice “experience” and “context.”\n\n“In different contexts, you don’t just want a flat, monotonous voice,” Harris said. “If you’re in a customer support experience and you want the voice to be apologetic because it’s made a mistake, you can actually have the voice have that emotion in it … Our big belief, here, is that developers and users want to really control not just what is spoken, but how things are spoken.”\n\nAs for OpenAI’s new speech-to-text models, “gpt-4o-transcribe” and “gpt-4o-mini-transcribe,” they effectively replace the company’s long-in-the-tooth Whisper transcription model. Trained on “diverse, high-quality audio datasets,” the new models can better capture accented and varied speech, OpenAI claims, even in chaotic environments.\n\nThey’re also less likely to hallucinate, Harris added. Whisper notoriously tended to fabricate words — and even whole passages — in conversations, introducing everything from racial commentary to imagined medical treatments into transcripts.\n\n“[T]hese models are much improved versus Whisper on that front,” Harris said. “Making sure the models are accurate is completely essential to getting a reliable voice experience, and accurate [in this context] means that the models are hearing the words precisely [and] aren’t filling in details that they didn’t hear.”\n\nYour mileage may vary depending on the language being transcribed, however.\n\nAccording to OpenAI’s internal benchmarks, gpt-4o-transcribe, the more accurate of the two transcription models, has a “word error rate” approaching 30% (out of 120%) for Indic and Dravidian languages such as Tamil, Telugu, Malayalam, and Kannada. That means three out of every 10 words from the model will differ from a human transcription in those languages.\n\nTHE RESULTS FROM OPENAI TRANSCRIPTION BENCHMARKING.\nIMAGE CREDITS:OPENAI\n\nIn a break from tradition, OpenAI doesn’t plan to make its new transcription models openly available. The company historically released new versions of Whisper for commercial use under an MIT license.\n\nHarris said that gpt-4o-transcribe and gpt-4o-mini-transcribe are “much bigger than Whisper” and thus not good candidates for an open release.\n\n“[T]hey’re not the kind of model that you can just run locally on your laptop, like Whisper,” he continued. “[W]e want to make sure that if we’re releasing things in open source, we’re doing it thoughtfully, and we have a model that’s really honed for that specific need. And we think that end-user devices are one of the most interesting cases for open-source models.”\n\nUpdated March 20, 2025, 11:54 a.m. PT to clarify the language around word error rate and updated the benchmark results chart with a more recent version.
"""
load_dotenv()
techArticleService = TechArticleService()
def run():
    """
    Run the crew.
    """
    # inputs = {
    #     'title': title,
    #     'content': content
    # }
    
    try:
        articles = techArticleService.get_articles(is_cn_generted=False)
        for article in articles:
            inputs = {
                'title': article['title'],
                'content': article['content']['content']
            }  
            output = NoteMind().crew().kickoff(inputs=inputs)
            markdownContent = output.raw
            techArticleService.update_cn_article(article['id'], markdownContent)
            print(f"✅ 文章《{article['title']}》已成功保存！")
        
    except Exception as e:
        raise Exception(f"An error occurred while running the crew: {e}")


def train():
    """
    Train the crew for a given number of iterations.
    """
    inputs = {
        "topic": "AI LLMs"
    }
    try:
        NoteMind().crew().train(n_iterations=int(sys.argv[1]), filename=sys.argv[2], inputs=inputs)

    except Exception as e:
        raise Exception(f"An error occurred while training the crew: {e}")

def replay():
    """
    Replay the crew execution from a specific task.
    """
    try:
        NoteMind().crew().replay(task_id=sys.argv[1])

    except Exception as e:
        raise Exception(f"An error occurred while replaying the crew: {e}")

def test():
    """
    Test the crew execution and returns the results.
    """
    inputs = {
        "topic": "AI LLMs"
    }
    try:
        NoteMind().crew().test(n_iterations=int(sys.argv[1]), openai_model_name=sys.argv[2], inputs=inputs)

    except Exception as e:
        raise Exception(f"An error occurred while testing the crew: {e}")

if __name__ == "__main__":
    run()